home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / explode / config.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  4KB  |  164 lines

  1. /*
  2.  * config.c - installs new explosion path in explode executable
  3.  *
  4.  * Bruno Costa - 30 Dec 89 - 31 Dec 89
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "common.h"
  11.  
  12. #ifndef SEEK_END
  13. #define SEEK_SET 0
  14. #define SEEK_CUR 1
  15. #define SEEK_END 2
  16. #endif
  17.  
  18. #define TRUE 1
  19. #define FALSE 0
  20.  
  21. struct {
  22.  unsigned long int code;
  23.  char tag[TAGSIZE];
  24.  int timedelay, pattern, nsteps;
  25.  struct {
  26.    struct {
  27.      int x, y;
  28.    } topleft, botright;
  29.  } step[MAXSTEPS];
  30. } global = {
  31.  TAG_CODE,
  32.  TAG_ID,
  33.  DEFDELAY, DEFPAT, 21,
  34.  {
  35.   {    0,    0,    0,    0 },    /* left, top, right, bottom */
  36.   {  135,  -22,  135,  -22 },
  37.   {  265,  -30,  265,  -30 },
  38.   {  389,  -25,  389,  -25 },
  39.   {  506,   -6,  506,   -6 },
  40.   {  615,   23,  615,   23 },
  41.   {  717,   62,  717,   62 },
  42.   {  811,  111,  811,  111 },
  43.   {  895,  168,  895,  168 },
  44.   {  971,  231,  971,  231 },
  45.   { 1036,  300, 1036,  300 },
  46.   { 1090,  373, 1090,  373 },
  47.   { 1134,  449, 1134,  449 },
  48.   { 1166,  527, 1166,  527 },
  49.   { 1186,  605, 1186,  605 },
  50.   { 1194,  683, 1194,  683 },
  51.   { 1188,  759, 1188,  759 },
  52.   { 1169,  833, 1169,  833 },
  53.   { 1135,  902, 1135,  902 },
  54.   { 1087,  966, 1087,  966 },
  55.   { 1024, 1024, 1024, 1024 },    /* 21th entry */
  56.   {    0,    0,    0,    0 },
  57.   {    0,    0,    0,    0 },
  58.   {    0,    0,    0,    0 },
  59.   {    0,    0,    0,    0 },
  60.   {    0,    0,    0,    0 },
  61.   {    0,    0,    0,    0 },
  62.   {    0,    0,    0,    0 },
  63.   {    0,    0,    0,    0 },
  64.   {    0,    0,    0,    0 }
  65.  }
  66. };
  67. /*
  68.  *  The points above were generated using an HP-28S and a bezier curve
  69.  * plotting program. The curve was broken in 20 line segments and the
  70.  * control points were: (0, 0) ; (922, -205) ; (1587, 665) ; (1024, 1024)
  71.  */
  72.  
  73. void error (char *msg)
  74. {
  75.  fputs ("config: ", stderr);
  76.  fputs (msg, stderr);
  77.  fputc ('\n', stderr);
  78.  exit (20);
  79. }
  80.  
  81. void main (int argc, char *argv[])
  82. {
  83.  FILE *f;
  84.  char *buffer, *p, *q, *match;
  85.  int tagfound, i;
  86.  long size;
  87.  
  88.  puts ("\x1b[33mConfig 1.2b\x1b[31m - \xa9 1989 by Bruno Costa");
  89.  if (argc != 2)
  90.  {
  91.    puts ("installs a motion path read from stdin in given Explode 1.2b executable.");
  92.    puts ("usage: \x1b[1mconfig\x1b[0m <executable>");
  93.    exit (5);
  94.  }
  95.  
  96.  /*
  97.   * fill in global struct with data from stdin
  98.   */
  99.  puts ("reading new data ...");
  100.  
  101.  scanf ("%d %x", &global.timedelay, &global.pattern);
  102.  i = 0;
  103.  while (scanf("%d %d %d %d", &global.step[i].topleft.x, &global.step[i].topleft.y,
  104.                &global.step[i].botright.x, &global.step[i].botright.y) == 4)
  105.    if (++i >= MAXSTEPS)
  106.      error ("too many steps");
  107.  
  108.  if (i)
  109.    global.nsteps = i;
  110.  
  111.  puts ("installing ...");
  112.  
  113.  /*
  114.   * read original file in buffer
  115.   */
  116.  f = fopen (argv[1], "rb+");    /* binary read and/or write */
  117.  if (!f)
  118.    error ("file not found");
  119.  
  120.  fseek (f, 0, SEEK_END);    /* go to EOF ... */
  121.  size = ftell (f);        /* ... and get file size */
  122.  rewind (f);
  123.  
  124.  if (size <= 0  ||  !(buffer = malloc (size)))
  125.    error ("out of memory");
  126.  
  127.  if (fread (buffer, size, 1, f) != 1)
  128.    error ("could not read file");
  129.  
  130.  /* 
  131.   * search for tag in buffer
  132.   */
  133.  tagfound = FALSE;
  134.  match = (char *)&global.code;
  135.  for (p = buffer, q = buffer + size; p < q  && !tagfound; p++)
  136.    if (*p == *match)
  137.      tagfound = (*match++) ? FALSE : TRUE;
  138.    else
  139.      match = (char *)&global.code;
  140.  
  141.  if (!tagfound)
  142.    error ("no tag found");
  143.  
  144.  /*
  145.   * copy new information
  146.   */
  147.  for (q = global.tag + sizeof (global); match < q; p++, match++)
  148.    *p = *match;
  149.  
  150.  /*
  151.   * write out new program
  152.   */
  153.  rewind (f);
  154.  
  155.  if (fwrite (buffer, size, 1, f) != 1)
  156.    error ("could not write file");
  157.  
  158.  fclose (f);
  159.  free (buffer);
  160.  
  161.  puts ("done.");
  162.  exit (0);
  163. }
  164.